layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/market/cloud/supplier/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'id', title: 'ID'}
      ,{field: 'name', title: '供应商名称'}
      ,{field: 'accountId', title: '云支付ID'}
      ,{field: 'userName', title: '供应商姓名'}
	  ,{field: 'userPhone', title: '供应商手机号'}
	  ,{field: 'serviceChargeRatio', title: '服务费比例'}
      ,{field: 'stateName', width: 80, title: '状态'}
      ,{field: 'createDate', title: '创建时间', sort: true}
      ,{title: '操作', width: 240, align:'center', fixed: 'right',templet:function (record){
        var html = ' <a class="layui-btn layui-btn-xs" lay-event="info">详情</a>';
		html = html + ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="goodsList">商品列表</a>';
        if(record.state == 1){
            html = html + '<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="updateState">启用</a>';
        }else{
			html = html + '<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="updateState">关闭</a>';
		}
        html = html + ' <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
        return html
      }}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
    });
  });
  
  //事件
  var active = {
    add: function(){
      layer.open({
        type: 2
        ,title: '新增供货商'
        ,content: layui.setter.webRoot + '/market/cloud/supplier/add.html'
        ,maxmin: false
        ,area: ['500px', '400px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-form-submit'
          ,submit = layero.find('iframe').contents().find('#'+ submitID);

          //监听提交
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field; //获取提交的字段
        	$.ajax({
	    		type: "post",
	            url: layui.setter.webRoot + '/market/cloud/supplier/add.do',
	            data:field,
				dataType:"json",
				success:function(res){
					if(res.code == 200){
						table.reload('LAY-table'); //数据刷新
						layer.close(index); //关闭弹层
					}else{
						layer.msg(res.msg,{icon: 2});
					}
				}
	        });
          });  
          submit.trigger('click');
        }
      }); 
    }
    ,info:function(obj){
          layer.open({
              type: 2
              ,title: '查看供货商信息'
              ,content: layui.setter.webRoot + '/market/cloud/supplier/info.html?id='+obj.data.id
              ,maxmin: true
              ,area: ['90%', '80%']
          });
      }
	  ,goodsList:function(obj){
          layer.open({
              type: 2
              ,title: obj.data.name + '商品列表'
              ,content: layui.setter.webRoot + '/market/cloud/supplier/goods/list.html?supplierId='+obj.data.id
              ,maxmin: true
              ,area: ['98%', '98%']
          });
       }
	  ,updateState:function(obj){
			var msg = '确定要关闭供应商吗？';
			if(obj.data.state == 1){
				msg = '确定要启用供应商吗？';
			}
	      	layer.confirm(msg, function(index){
	  	    	$.ajax({
	  	      	  type: "post",
	  	          url: layui.setter.webRoot + '/market/cloud/supplier/state/update.do',
	  	          data:{id:obj.data.id},
	  	  		  dataType:"json",
	  	  		  success:function(res){
	  	  			if(res.code == 200){
	  	  				table.reload('LAY-table'); //数据刷新
						layer.msg('操作成功',{icon: 1});
	  	  			}else{
	  	  				layer.msg(res.msg,{icon: 5});
	  	  			}
	  	  		  }
	  	        });
	  	    	layer.close(index);
	  	    });
	      }
    ,del:function(obj){
    	layer.confirm('确定要删除该供货商吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/market/cloud/supplier/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.layui-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('market_supplier_list', {})
});